package com.example.lib_car

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.lib_car.databinding.ActivityAllcarBinding
import com.example.lib_common.mvi.BaseActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.ceil

@Route(path = "/car/ActivityParking")
class ActivityParking : BaseActivity<ActivityAllcarBinding,ParkingViewModel>() ,OnItemClickListener{
    private var allSpace : ArrayList<ParkingEntity> = ArrayList()
    val parkingAdapter = ParkingAdapter()
    var index=0
    override fun createVM(): ParkingViewModel {
        return viewModels<ParkingViewModel>().value
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityAllcarBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindState()
        vm.parkingAll()
        v.include.backBtn.setOnClickListener { finish() }
        EventBus.getDefault().register(this)
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refreshParking(string: String){
        if (string=="refreshCar"){
            parkingAdapter.notifyItemChanged(index)
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    private fun bindState() {
        lifecycleScope.launch (Dispatchers.Main){
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    if (it.entity.statuesCode==200){
                        allSpace= it.entity.values as ArrayList<ParkingEntity>
                        initData()
                    }
                }
            }
        }
    }
//    ===========车位列表
    private fun initData() {
        if (allSpace.size!=0){
            var parkList : ArrayList<ParkingEntity> =ArrayList()
            parkList.addAll(allSpace)
            val count = ceil(allSpace.size/5.0).toInt()
            var all = allSpace.size+count
            var index=0
            var i=count
            for(j in 1..all){
                if (index%6==0){
                    var park =ParkingEntity()
                    parkList.add(index,park)
                }
                index++
            }
            var a=0
            var line=0
            var num=1
            for (parkingEntity in parkList) {
                if (a%6==0){
                    parkingEntity.itemType=1
                    parkingEntity.parkLeft=i
                    line=i
                    i--
                    num=1
                }else{
                    parkingEntity.parkNum="1-"+line+"0"+num
                    num++
                }
                a++
            }
            var layoutManager =GridLayoutManager(this,6)
            v.parkingallRv.layoutManager=layoutManager
            parkingAdapter.data=parkList
            v.parkingallRv.adapter=parkingAdapter
            parkingAdapter.setOnItemClickListener(this)

        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        var list = adapter.data as ArrayList<ParkingEntity>
        if (list[position].parkUser==0L){
            ARouter.getInstance().build("/car/ActivityApplyParking").withLong("parkId",list.get(position).parkId)
                .withString("parkName",list[position].parkName).navigation()
            index=position
        }else{

        }
    }
}